package xipujewel.xipujewel.service;

import java.text.DecimalFormat;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import xipujewel.xipujewel.entity.account.User;
import xipujewel.xipujewel.entity.app.Jewel;
import xipujewel.xipujewel.entity.app.Order;
import xipujewel.xipujewel.entity.app.Ring;
import xipujewel.xipujewel.repository.jpa.OrderDao;
import xipujewel.xipujewel.service.common.JpaManager;

@Component
@Transactional
public class OrderManager extends JpaManager<Order, OrderDao> {

	private static Logger logger = LoggerFactory.getLogger(OrderManager.class);

	private final static String[] orderJewelTitle1 = { "货号", "形状", "重量", "颜色", "净度", "切工", "抛光", "对称", "荧光", "证书",
			"尺寸", "国际报价", "折扣", "人民币价格", "二级折扣", "二级批发价", "库存", "证书号" };//钻石标题（由二级零售下的订单）

	private final static String[] orderJewelTitle2 = { "货号", "形状", "重量", "颜色", "净度", "切工", "抛光", "对称", "荧光", "证书",
			"尺寸", "国际报价", "折扣", "人民币价格", "库存", "证书号" };//钻石标题（非二级零售下的订单）

	private final static String[] orderRingTitle1 = { "名称", "款号", "主钻重量", "主钻形状", "金重", "材质", "指圈", "价格", "二级批发价" };//戒托标题（由二级零售下的订单）

	private final static String[] orderRingTitle2 = { "名称", "款号", "主钻重量", "主钻形状", "金重", "材质", "指圈", "价格" };//戒托标题（非二级零售下的订单）

	@Autowired
	private OrderDao orderDao;

	/**
	 *  先删除中间表，再删除订单记录
	 * @param id
	 */
	public void deleteOrder(Long id) {
		Session session = (Session) entityManager.getDelegate();
		String queryString1 = "delete from order_jewel where order_id=" + id;
		String queryString2 = "delete from order_ring where order_id=" + id;
		session.createSQLQuery(queryString1).executeUpdate();
		session.createSQLQuery(queryString2).executeUpdate();
		orderDao.delete(id);
	}

	/**
	 * 用于导出所下的单
	 * @param jewels
	 * @param user
	 * @return
	 */
	public XSSFWorkbook getEXL(List<Jewel> jewels, List<Ring> rings, User user) {
		try {
			//			int totalRow = 0;
			// 声明一个工作薄
			XSSFWorkbook workbook = new XSSFWorkbook();
			if (jewels != null && jewels.size() != 0) {
				if (user.getType() == 1) {
					loadJewelSecond(jewels, workbook);
				} else {
					loadJewel(jewels, workbook);
				}
			}

			if (rings != null && rings.size() != 0) {
				if (user.getType() == 1) {
					loadRingSecond(rings, workbook);
				} else {
					loadRing(rings, workbook);
				}
			}
			/*bodyRow = xssfSheet.createRow(jewels.size() + 2);
			cell = bodyRow.createCell(0);
			cell.setCellValue("所属会员");
			cell = bodyRow.createCell(1);
			cell.setCellValue("地址");
			cell = bodyRow.createCell(2);
			cell.setCellValue("联系方式");
			bodyRow = xssfSheet.createRow(jewels.size() + 3);
			cell = bodyRow.createCell(0);
			cell.setCellValue(user.getNo());
			cell = bodyRow.createCell(1);
			cell.setCellValue(user.getShopAddress().getProv() + "," + user.getShopAddress().getCity() + ","
					+ user.getShopAddress().getCounty() + "," + user.getShopAddress().getAddress());
			cell = bodyRow.createCell(2);
			cell.setCellValue(user.getShopAddress().getCellphone());*/
			return workbook;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 载入二级零售商的钻石数据
	 * @param jewels
	 * @param workbook
	 */
	private void loadJewelSecond(List<Jewel> jewels, Workbook workbook) {
		// 生成一个表格
		Sheet xssfSheet = workbook.createSheet("钻石订单");
		Row bodyRow = xssfSheet.createRow(0);
		Cell cell;

		for (int i = 0; i < orderJewelTitle1.length; i++) {//处理标题
			cell = bodyRow.createCell(i);
			cell.setCellValue(orderJewelTitle1[i]);
		}
		for (int j = 0; j < jewels.size(); j++) {
			bodyRow = xssfSheet.createRow(j + 1);
			for (int i = 0; i < orderJewelTitle1.length; i++) {//处理标题
				cell = bodyRow.createCell(i);
				switch (i) {
				case 0:
					cell.setCellValue(jewels.get(j).getNum());//货号
					break;
				case 1:
					//						xssfSheet.setColumnWidth((short) 1, (short) (300 * 35));
					cell.setCellValue(jewels.get(j).getShape());//形状
					break;
				case 2:
					cell.setCellValue("" + jewels.get(j).getWeight());//重量
					break;
				case 3:
					cell.setCellValue(jewels.get(j).getColor());//颜色
					break;
				case 4:
					cell.setCellValue(jewels.get(j).getNeatness());//净度
					break;
				case 5:
					cell.setCellValue(jewels.get(j).getCut());//切工
					break;
				case 6:
					cell.setCellValue(jewels.get(j).getBuffing());//抛光	
					break;
				case 7:
					cell.setCellValue(jewels.get(j).getSymmetry());//对称
					break;
				case 8:
					cell.setCellValue(jewels.get(j).getFluorescence());//荧光
					break;
				case 9:
					cell.setCellValue(jewels.get(j).getCard());//证书
					break;
				case 10:
					cell.setCellValue(jewels.get(j).getSize());//尺寸
					break;
				case 11:
					cell.setCellValue(jewels.get(j).getInternationalPrice());//国际报价
					break;
				case 12:
					cell.setCellValue("" + jewels.get(j).getDiscount());//折扣
					break;
				case 13:
					cell.setCellValue(jewels.get(j).getPriceRMB());//人民币价格
					break;
				case 14:
					DecimalFormat fnum = new DecimalFormat("##0.00");
					float dis2 = Float.parseFloat(jewels.get(j).getDiscount2());
					String discount2 = fnum.format(dis2);
					cell.setCellValue(discount2);//二级折扣
					break;
				case 15:
					DecimalFormat fnum2 = new DecimalFormat("##0");
					String secondPrice = fnum2.format(jewels.get(j).getSecondPrice());
					cell.setCellValue(secondPrice);//二级批发价
					break;
				case 16:
					cell.setCellValue(jewels.get(j).getHoldType());//库存
				case 17:
					cell.setCellValue(jewels.get(j).getCardId());//证书号
					break;
				default:
					break;
				}
			}
		}
	}

	/**
	 * 载入二级零售商的戒托数据
	 * @param rings
	 * @param workbook
	 */
	private void loadRingSecond(List<Ring> rings, Workbook workbook) {
		// 生成一个表格
		/*if (totalRow != 0) {
			bodyRow = xssfSheet.createRow(totalRow + 2);
		}*/
		// 生成一个表格
		Sheet xssfSheet2 = workbook.createSheet("戒托订单");
		Row bodyRow = xssfSheet2.createRow(0);
		Cell cell;
		for (int i = 0; i < orderRingTitle1.length; i++) {//处理标题
			cell = bodyRow.createCell(i);
			cell.setCellValue(orderRingTitle1[i]);
		}
		for (int j = 0; j < rings.size(); j++) {
			bodyRow = xssfSheet2.createRow(j + 1);
			for (int i = 0; i < orderJewelTitle1.length; i++) {//处理标题
				cell = bodyRow.createCell(i);
				switch (i) {
				case 0:
					cell.setCellValue(rings.get(j).getName());//名称
					break;
				case 1:
					cell.setCellValue(rings.get(j).getNo());//款号
					break;
				case 2:
					cell.setCellValue(rings.get(j).getMainBeginWeigh() + "--" + rings.get(j).getMainEndWeigh());//主钻重量
					break;
				case 3:
					cell.setCellValue(rings.get(j).getMainShape());//主钻形状
					break;
				case 4:
					cell.setCellValue(rings.get(j).getGoldWeight());//金重
					break;
				case 5:
					cell.setCellValue(rings.get(j).getQuality());//材质
					break;
				case 6:
					cell.setCellValue(rings.get(j).getSizeBegin() + "--" + rings.get(j).getSizeEnd());//指圈
					break;
				case 7:
					cell.setCellValue(rings.get(j).getPrice());//价格
					break;
				case 8:
					cell.setCellValue(rings.get(j).getSecondPrice());//二级批发价
					break;
				default:
					break;
				}
			}
		}
	}

	/**
	 * 载入非二级零售商的钻石数据
	 * @param jewels
	 * @param workbook
	 */
	private void loadJewel(List<Jewel> jewels, Workbook workbook) {
		// 生成一个表格
		Sheet xssfSheet = workbook.createSheet("钻石订单");
		Row bodyRow = xssfSheet.createRow(0);
		Cell cell;

		for (int i = 0; i < orderJewelTitle2.length; i++) {//处理标题
			cell = bodyRow.createCell(i);
			cell.setCellValue(orderJewelTitle2[i]);
		}
		for (int j = 0; j < jewels.size(); j++) {
			bodyRow = xssfSheet.createRow(j + 1);
			for (int i = 0; i < orderJewelTitle2.length; i++) {//处理标题
				cell = bodyRow.createCell(i);
				switch (i) {
				case 0:
					cell.setCellValue(jewels.get(j).getNum());//货号
					break;
				case 1:
					//						xssfSheet.setColumnWidth((short) 1, (short) (300 * 35));
					cell.setCellValue(jewels.get(j).getShape());//形状
					break;
				case 2:
					cell.setCellValue("" + jewels.get(j).getWeight());//重量
					break;
				case 3:
					cell.setCellValue(jewels.get(j).getColor());//颜色
					break;
				case 4:
					cell.setCellValue(jewels.get(j).getNeatness());//净度
					break;
				case 5:
					cell.setCellValue(jewels.get(j).getCut());//切工
					break;
				case 6:
					cell.setCellValue(jewels.get(j).getBuffing());//抛光	
					break;
				case 7:
					cell.setCellValue(jewels.get(j).getSymmetry());//对称
					break;
				case 8:
					cell.setCellValue(jewels.get(j).getFluorescence());//荧光
					break;
				case 9:
					cell.setCellValue(jewels.get(j).getCard());//证书
					break;
				case 10:
					cell.setCellValue(jewels.get(j).getSize());//尺寸
					break;
				case 11:
					cell.setCellValue(jewels.get(j).getInternationalPrice());//国际报价
					break;
				case 12:
					cell.setCellValue("" + jewels.get(j).getDiscount());//折扣
					break;
				case 13:
					cell.setCellValue(jewels.get(j).getPriceRMB());//人民币价格
					break;
				case 14:
					cell.setCellValue(jewels.get(j).getHoldType());//库存
					break;
				case 15:
					cell.setCellValue(jewels.get(j).getCardId());//证书号
					break;
				default:
					break;
				}
			}
		}
	}

	/**
	 * 载入非二级零售商的戒托数据
	 * @param rings
	 * @param workbook
	 */
	private void loadRing(List<Ring> rings, Workbook workbook) {
		// 生成一个表格
		/*if (totalRow != 0) {
			bodyRow = xssfSheet.createRow(totalRow + 2);
		}*/
		// 生成一个表格
		Sheet xssfSheet2 = workbook.createSheet("戒托订单");
		Row bodyRow = xssfSheet2.createRow(0);
		Cell cell;
		for (int i = 0; i < orderRingTitle2.length; i++) {//处理标题
			cell = bodyRow.createCell(i);
			cell.setCellValue(orderRingTitle2[i]);
		}
		for (int j = 0; j < rings.size(); j++) {
			bodyRow = xssfSheet2.createRow(j + 1);
			for (int i = 0; i < orderRingTitle2.length; i++) {//处理标题
				cell = bodyRow.createCell(i);
				switch (i) {
				case 0:
					cell.setCellValue(rings.get(j).getName());//名称
					break;
				case 1:
					cell.setCellValue(rings.get(j).getNo());//款号
					break;
				case 2:
					cell.setCellValue(rings.get(j).getMainBeginWeigh() + "--" + rings.get(j).getMainEndWeigh());//主钻重量
					break;
				case 3:
					cell.setCellValue(rings.get(j).getMainShape());//主钻形状
					break;
				case 4:
					cell.setCellValue(rings.get(j).getGoldWeight());//金重
					break;
				case 5:
					cell.setCellValue(rings.get(j).getQuality());//材质
					break;
				case 6:
					cell.setCellValue(rings.get(j).getSizeBegin() + "--" + rings.get(j).getSizeEnd());//指圈
					break;
				case 7:
					cell.setCellValue(rings.get(j).getPrice());//价格
					break;
				default:
					break;
				}
			}
		}
	}

	/**
	 *  先删除中间表，再删除订单记录
	 * @param id
	 */
	public void deleteOrders(List<Long> ids) {
		for (Long id : ids) {
			deleteOrder(id);
		}
	}

	@Override
	protected OrderDao getDao() {
		return orderDao;
	}

}
